<template>
  <div :id="id" class="ys-popup" :style="{ width: popupWidth,height:popupHeight, left: 'calc(-' + popupWidth + '/2)' }">
    <div class="popupPage">
      <div class="pop-title" v-if="popupHeader">
        <div class="popTitle-left">
          <div class="p1">{{ popupTitle.popuptit }}</div>
          <div class="p2">{{ popupTitle.popuptim }}</div>
        </div>
        <div class="close" @click="closePopup">×</div>
      </div>
      <div class="popContent" :style="{ height: popupTitle ? 'calc(100% - 30px)' : '100%' }">
        <slot></slot>

        <div>{{ popupTitle.popupcon }}</div>

      </div>
    </div>
  </div>
</template>

<script>
import Overlay from 'ol/Overlay';

export default {
  data() {
    return {
      overlay: null
    }
  },
  props: {
    position: Array,
    map: Object,
    id: String,
    //气泡宽度
    popupWidth: {
      type: String,
      default: '2rem'
    },
    //气泡高度
    popupHeight: {
      type: String,
      default: '2rem'
    },
    //是否显示气泡头部的title
    popupHeader: {
      type: Boolean,
      default: true
    },
    //气泡title内容
    popupTitle: {
      type: Object,
      default: {}
    }
  },
  mounted() {
    if (this.map && !this.overlay) {
      this.overlay = new Overlay({
        element: document.getElementById(this.id),
        autoPan: true,
        autoPanAnimation: {
          duration: 250
        }
      });
      this.overlay.setPosition(undefined);
      // this.map.map.addOverlay(this.overlay);
      this.$emit("addOverlay", this.overlay)
      // this.map.popup = this.overlay;
    }
  },
  watch: {
    position(newP) {
      this.overlay.setPosition(newP);
      this.overlay.setPositioning("bottom-left");
    },
    map(map) {
      if (map && !this.overlay) {
        this.overlay = new Overlay({
          element: document.getElementById(this.id),
          autoPan: true,
          autoPanAnimation: {
            duration: 250
          }
        });
        this.overlay.setPosition(undefined);
        // this.map.map.addOverlay(this.overlay);
        this.$emit("addOverlay", this.overlay)
        // this.map.popup = this.overlay;
      }
    }
  },
  methods: {
    closePopup() {
      this.overlay.setPosition(undefined);
      return false;
    }
  }
}
</script>
<style lang="scss" scoped>
.ys-popup {
  position: absolute;
  left: 0;
  bottom: 0.1rem;
  // border: 0.01rem solid #01C5FE;
}

.popupPage {
  width: 100%;
  height: 100%;
  position: relative;

  .pop-title {
    display: flex;
    align-items: center;
    background-color: #5881FA;
    // box-shadow:#01C5FE 0rem 0rem 0.18rem inset;
    // border-bottom: 0.01rem solid #01C5FE;

    .popTitle-left {
      width: calc(100% - 0.3rem);
      height: 0.3rem;
      line-height: 0.3rem;
      color: #fff;
      font-size: 14px;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      // padding: 0 0.2rem 0 0.12rem;
      cursor: default;
      box-sizing: border-box;
      // text-align: left;
      
      display: flex;
      justify-content: space-between;
      .p1{
        text-indent: 2em;
      }
    }

    .close {
      width: 0.3rem;
      text-align: center;
      font-size: 0.18rem;
      color: #fff;
      cursor: pointer;

    }
  }

  .popContent {
    height: calc(100% - 0.3rem);
    width: 100%;
    background-color: #fff;
    position: relative;
    font-size: 20px;
    color: #000;
    text-indent: 2em;

  }

  // .popContent:before {
  //   content: "";
  //   position: absolute;
  //   width: 0;
  //   height: 0;
  //   top: 100%;
  //   left: calc(50% - 0.1rem);
  //   border-top: 0.2rem solid rgb(255, 255, 255);
  //   border-left: 0.1rem solid transparent;
  //   border-right: 0.1rem solid transparent;
  // }
}

.ys-popup:after,
.ys-popup:before {
  top: 100%;
  border: solid transparent;
  content: " ";
  height: 0;
  width: 0;
  position: absolute;
  pointer-events: none;

}

.ys-popup:after {
  border-width: 0.1rem;
  left: 0.48rem;
  margin-left: -0.1rem;

}

.ys-popup:before {
  /*border-top-color: #FFFFFF;*/
  border-width: 0.11rem;
  left: 0.48rem;
  margin-left: -0.11rem;

}

.ys-popup-closer {
  text-decoration: none;
  position: absolute;
  top: 0.05rem;
  right: 0.08rem;
  color: #ffffff;

}

.ys-popup-closer:after {
  content: "✖";
}
</style>
